// shopPages/searchView/searchView.js
var sliderWidth = 55;
var app = getApp()
var gdlist = []
var jllist = []
var splist = []
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabs: ["商品", "尽猎优选", "店铺"],
    activeIndex: 0,
    sliderOffset: 0,
    sliderLeft: 24,
    gd_page: 1,
    gd_more_status: false,
    jl_page: 1,
    jl_more_status: false,
    sp_page: 1,
    sp_more_status: false,
    goodsList: [],
    jlList: [],
    spList: [],
    loading:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    gdlist = []
    jllist = []
    splist = []
    // 选项卡
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        that.setData({
          sliderLeft: (res.windowWidth / that.data.tabs.length - sliderWidth) / 2,
          sliderOffset: res.windowWidth / that.data.tabs.length * that.data.activeIndex
        });
      }
    })

    that.setData({
      keywords: options.keywords
    });
    that.getGoodsList(options.keywords)
  },
  //获取商品列表
  getGoodsList: function(keywords) {
    var that = this;
    var url2 = '/mall/search_index';
    var param = {};
    param.pageno = that.data.gd_page;
    param.pagesize = 20;
    param.keywords = keywords;
    param.sort = 'sort desc';
    wx.showLoading({
      title: '玩命加载中',
    })
    var gd_more_status = that.data.gd_more_status;
    // console.log(param);return false;
    app.HttpPostSend(url2, param, function(res) {
      that.setData({
         loading:false
      })
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
        gd_more_status = false;
        that.setData({
          goodsList: []
        })
      } else {
        var goodsList = res.data;
        var gd_page = that.data.gd_page;
        if (gd_page == 1) {
          gdlist = [];
        }
        for (let i = 0; i < goodsList.length; i++) {
          gdlist.push(goodsList[i]);
        }
        if (goodsList.length < 20) {
          gd_more_status = false;
        } else {
          gd_more_status = true;

          gd_page += 1;
          that.setData({
            gd_page: gd_page
          });
        }
        that.setData({
          goodsList: gdlist,
          gd_more_status: gd_more_status
        })

      }
      wx.hideLoading();
    });
  },
  getJlList: function(keywords) {
    var that = this;

    var url2 = '/mall/index_jl';
    var param = {};
    param.pageno = that.data.jl_page;
    param.pagesize = 20;
    param.keywords = keywords;
    param.sort = 'sort desc';
    param.city_code = app.globalData.currentCode;
    wx.showLoading({
      title: '玩命加载中',
    })
    var jl_more_status = that.data.jl_more_status;
    // console.log(param);return false;
    app.HttpPostSend(url2, param, function(res) {
      that.setData({
        loading: false
      })
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
        jl_more_status = false;
        that.setData({
          jlList: []
        })
      } else {
        var jlList = res.data;
        var jl_page = that.data.jl_page;
        if (jl_page == 1) {
          jllist = [];
        }
        for (let i = 0; i < jlList.length; i++) {
          jllist.push(jlList[i]);
        }
        if (jlList.length < 20) {
          jl_more_status = false;
        } else {
          jl_more_status = true;

          jl_page += 1;
          that.setData({
            jl_page: jl_page
          });
        }
        that.setData({
          jlList: jllist,
          jl_more_status: jl_more_status
        })

      }
      wx.hideLoading();
    });
  },
  getSpList: function(keywords) {
    var that = this;

    var url2 = '/mall/index_shop';
    var param = {};
    param.pageno = that.data.sp_page;
    param.pagesize = 20;
    param.keywords = keywords;
    param.sort = 'integral desc';
    param.latitude = app.globalData.latitude;
    param.longitude = app.globalData.longitude;
    param.city_code = app.globalData.currentCode;
    wx.showLoading({
      title: '玩命加载中',
    })
    var sp_more_status = that.data.sp_more_status;
    // console.log(param);return false;
    app.HttpPostSend(url2, param, function(res) {
      that.setData({
        loading: false
      })
      if (res.msg != "success") {
        app.showTipMsg(res.msg);
        sp_more_status = false;
        that.setData({
          spList: []
        })
      } else {
        var spList = res.data;
        var sp_page = that.data.sp_page;
        if (sp_page == 1) {
          splist = [];
        }
        for (let i = 0; i < spList.length; i++) {
          splist.push(spList[i]);
        }
        if (spList.length < 20) {
          sp_more_status = false;
        } else {
          sp_more_status = true;

          sp_page += 1;
          that.setData({
            sp_page: sp_page
          });
        }
        console.log(splist);
        that.setData({
          spList: splist,
          sp_more_status: sp_more_status
        })

      }
      wx.hideLoading();
    });
  },
  // 选项卡
  tabClick: function(e) {
    var that = this;
    that.setData({
      sliderOffset: e.currentTarget.offsetLeft,
      activeIndex: e.currentTarget.id
    });
    console.log(e.currentTarget.id);
    console.log(that.data.jl_more_status);
    console.log(that.data.jl_page);
    if (e.currentTarget.id == 0 && (that.data.gd_more_status == true || (that.data.gd_more_status == false && that.data.gd_page == 1))) {

      // 页数+1  
      that.getGoodsList(that.data.keywords);

    } else if (e.currentTarget.id == 1 && (that.data.jl_more_status == true || (that.data.jl_more_status == false && that.data.jl_page == 1))) {
      that.getJlList(that.data.keywords);
    } else if (e.currentTarget.id == 2 && (that.data.sp_more_status == true || (that.data.sp_more_status == false && that.data.sp_page == 1))) {
      that.getSpList(that.data.keywords);
    }
  },
  // 跳转个人商城
  toMyMall: function(e) {
    app.globalData.SwitchTabParams.v_id = e.currentTarget.dataset.uid;
    if (e.currentTarget.dataset.food_mall > 0) {
      wx.navigateTo({
        url: '/shopPages/shopxq/shopxq',
      })
    } else {
      wx.navigateTo({
        url: '/shopPages/personalShop/personalShop'
      })
    }
    // wx.navigateTo({
    //   url: '../../pages/myMall/myMall',
    // })

  },
  // 跳转商品详情
  toGoodsDetail_: function(e) {
    wx.navigateTo({
      url: "/pages/goodsDetail/goodsDetail?id=" + e.currentTarget.dataset.id
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.setData({
      loading: true
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    var that = this;
    var activeIndex = that.data.activeIndex;

    if (activeIndex == 0 && that.data.gd_more_status == true) {

      // 页数+1  
      that.getGoodsList(that.data.keywords);

    } else if (activeIndex == 1 && that.data.jl_more_status == true) {
      that.getJlList(that.data.keywords);
    } else if (activeIndex == 2 && that.data.sp_more_status == true) {
      that.getSpList(that.data.keywords);
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})